//
//  StudentPerformanceTableViewController.swift
//  StudentPerformance
//
//  Created by ap0stader on 2023/11/2.
//

import UIKit

class StudentPerformanceTableViewController : UITableViewController {
    var studentsInfo : StudentsInfo! = StudentsInfo()
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return studentsInfo.studentsCollection.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath) as! StudentCell
        
        let student = studentsInfo.studentsCollection[indexPath.row]
        
        cell.nameLabel.text = student.name
        cell.scoreLabel.text = "\(student.score)"
        cell.numberLabel.text = "\(student.id)"
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            let theStudent = studentsInfo.studentsCollection[indexPath.row]
            studentsInfo.deleteStudent(theStudent)
            tableView.deleteRows(at: [indexPath], with: .automatic)
        }
    }
    
    override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        studentsInfo.transferPosition(sourceIndex: sourceIndexPath.row, destinationIndex: destinationIndexPath.row)
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100;
    }
    
    @objc func refreshing() {
        if(refreshControl?.isRefreshing == true) {
            refreshControl?.attributedTitle = NSAttributedString("loading...")
            studentsInfo.addStudent()
            refreshControl?.endRefreshing()
            refreshControl?.attributedTitle = NSAttributedString("refresing")
            tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        // 新版本不写没有什么问题
        let statusBarHeight = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 30
        
        let insets = UIEdgeInsets(top: statusBarHeight, left: 0, bottom: 0, right: 0)
        
        tableView.contentInset = insets
        tableView.scrollIndicatorInsets = insets
        
        let theRefreshControl = UIRefreshControl()
        
        theRefreshControl.attributedTitle = NSAttributedString("refreshing")
        theRefreshControl.addTarget(self, action: #selector(refreshing), for: .valueChanged)
        
        refreshControl = theRefreshControl
    }
    
    @IBAction func shiftEditMode(_ sender: UIButton) {
        if isEditing {
            sender.setTitle("Edit Mode", for: .normal)
            setEditing(false, animated: true)
        } else {
            sender.setTitle("Submit", for: .normal)
            setEditing(true, animated: true)
        }
        
    }
    
    @IBAction func addStudent(_ sender: UIButton) {
        let newStudent = studentsInfo.addStudent()
        
        if let newIndex = studentsInfo.studentsCollection.firstIndex(of: newStudent) {
            let newIndexPath = IndexPath(row: newIndex, section: 0)
            
            tableView.insertRows(at: [newIndexPath], with: .automatic)
        }
    }
}
